﻿Imports System.Data
Imports System.Data.SqlClient

Namespace DB

    ''' <summary>
    ''' TypeName:    InventoryType
    ''' Author:      GeneratedCustomTemplate
    ''' DBTableName: STK_Inventory
    ''' </summary>
    ''' <remarks></remarks>

    Public Class InventoryType
        Inherits StockItemType

#Region "Declarations"
        'Protected Friend iInventoryId as Guid
        Protected Friend iStockItemId As Guid
        Protected Friend iRefType As String = "Manual_Stock_Entry"
        Protected Friend iRefId As Guid
        Protected Friend iCostCalculationMethod As String = "Replace_With_New_Cost"
        Protected Friend iPQuantity As Decimal
        Protected Friend iInventoryQuantity As Decimal
        Protected Friend iPCostPrice As Decimal
        Protected Friend iInventoryCostPrice As Decimal
        Protected Friend iPSellingPrice As Decimal
        Protected Friend iInventorySellingPrice As Decimal
        Protected Friend iPTaxPercentage As Decimal
        Protected Friend iInventoryTaxPercentage As Decimal
        Protected Friend iRemarks As String
        Protected Friend iAlteredByUser As String
        Protected Friend iAlteredDate As DateTime
        Protected Friend iSequence As Integer
#End Region

#Region "Constructors"
        Public Sub New()
            MyBase.New()
            Me.iTableName = TableNamesEnum.STK_Inventory
        End Sub
        Public Sub New(InventoryId As Guid)
            MyBase.New()
            Me.iTableName = TableNamesEnum.STK_Inventory
            Me.iId = InventoryId
        End Sub
        Public Sub New(InventoryId As Guid, baseLoadType As StockItemType)
            MyBase.new(baseLoadType)
            Me.iTableName = TableNamesEnum.STK_Inventory
            Me.iId = InventoryId
            Me.iStockItemId = baseLoadType.StockItemId
        End Sub
#End Region

#Region "Exposed Properties"
        Public Property InventoryId() As Guid
            Get
                Return Me.iId
            End Get
            Set(ByVal value As Guid)
                Me.iId = value
            End Set
        End Property
        Public Shadows Property StockItemId() As Guid
            Get
                Return Me.iStockItemId
            End Get
            Set(ByVal value As Guid)
                Me.iStockItemId = value
            End Set
        End Property
        Public Property RefType() As STK_InventoryMethodsEnum
            Get
                Return [Enum].Parse(GetType(STK_InventoryMethodsEnum), Me.iRefType.Trim().Replace(" ", "_"))
            End Get
            Set(ByVal value As STK_InventoryMethodsEnum)
                Me.iRefType = value.ToString.Replace("_", " ")
            End Set
        End Property
        Public Property RefId() As Guid
            Get
                Return Me.iRefId
            End Get
            Set(ByVal value As Guid)
                Me.iRefId = value
            End Set
        End Property
        Public Property PreviousQuantity() As Decimal
            Get
                Return Me.iPQuantity
            End Get
            Set(ByVal value As Decimal)
                Me.iPQuantity = value
            End Set
        End Property
        Public Property InventoryQuantity() As Decimal
            Get
                Return Me.iInventoryQuantity
            End Get
            Set(ByVal value As Decimal)
                Me.iInventoryQuantity = value
            End Set
        End Property
        Public Property CostCalculationMethod As STK_InventoryCostCalculationMethods
            Get
                Return [Enum].Parse(GetType(STK_InventoryCostCalculationMethods), Me.iCostCalculationMethod.Trim().Replace(" ", "_"))
            End Get
            Set(ByVal value As STK_InventoryCostCalculationMethods)
                Me.iCostCalculationMethod = value.ToString.Replace("_", " ")
            End Set
        End Property
        Public Property PreviousCostPrice() As Decimal
            Get
                Return Me.iPCostPrice
            End Get
            Set(ByVal value As Decimal)
                Me.iPCostPrice = value
            End Set
        End Property
        Public Property InventoryCostPrice() As Decimal
            Get
                Return Me.iInventoryCostPrice
            End Get
            Set(ByVal value As Decimal)
                Me.iInventoryCostPrice = value
            End Set
        End Property
        Public Property PreviousSellingPrice() As Decimal
            Get
                Return Me.iPSellingPrice
            End Get
            Set(ByVal value As Decimal)
                Me.iPSellingPrice = value
            End Set
        End Property
        Public Property InventorySellingPrice() As Decimal
            Get
                Return Me.iInventorySellingPrice
            End Get
            Set(ByVal value As Decimal)
                Me.iInventorySellingPrice = value
            End Set
        End Property
        Public Property PreviousTaxPercentage() As Decimal
            Get
                Return Me.iPTaxPercentage
            End Get
            Set(ByVal value As Decimal)
                Me.iPTaxPercentage = value
            End Set
        End Property
        Public Property InventoryTaxPercentage() As Decimal
            Get
                Return Me.iInventoryTaxPercentage
            End Get
            Set(ByVal value As Decimal)
                Me.iInventoryTaxPercentage = value
            End Set
        End Property
        Public Property Remarks() As String
            Get
                Return Me.iRemarks
            End Get
            Set(ByVal value As String)
                Me.iRemarks = value
            End Set
        End Property
        Public Property AlteredByUser() As String
            Get
                Return Me.iAlteredByUser
            End Get
            Set(ByVal value As String)
                Me.iAlteredByUser = value
            End Set
        End Property
        Public Property AlteredDate() As DateTime
            Get
                Return Me.iAlteredDate
            End Get
            Set(ByVal value As DateTime)
                Me.iAlteredDate = value
            End Set
        End Property
        Public Property Sequence() As Integer
            Get
                Return Me.iSequence
            End Get
            Set(ByVal value As Integer)
                Me.iSequence = value
            End Set
        End Property
#End Region

#Region "Implementations"
        Protected Friend Overloads Function Load(ByVal Row As DataRow) As Boolean
            If IsDBNull(Row(0)) Then Me.iId = Guid.Empty Else Me.iId = Row(0)
            If IsDBNull(Row(1)) Then Me.iStockItemId = Nothing Else Me.iStockItemId = Row(1)
            If IsDBNull(Row(2)) Then Me.iRefType = Nothing Else Me.iRefType = Row(2)
            If IsDBNull(Row(3)) Then Me.iRefId = Nothing Else Me.iRefId = Row(3)
            If IsDBNull(Row(4)) Then Me.iPQuantity = Nothing Else Me.iPQuantity = Row(4)
            If IsDBNull(Row(5)) Then Me.iInventoryQuantity = Nothing Else Me.iInventoryQuantity = Row(5)
            If IsDBNull(Row(6)) Then Me.iCostCalculationMethod = Nothing Else Me.iCostCalculationMethod = Row(6)
            If IsDBNull(Row(7)) Then Me.iPCostPrice = Nothing Else Me.iPCostPrice = Row(7)
            If IsDBNull(Row(8)) Then Me.iInventoryCostPrice = Nothing Else Me.iInventoryCostPrice = Row(8)
            If IsDBNull(Row(9)) Then Me.iPSellingPrice = Nothing Else Me.iPSellingPrice = Row(9)
            If IsDBNull(Row(10)) Then Me.iInventorySellingPrice = Nothing Else Me.iInventorySellingPrice = Row(10)
            If IsDBNull(Row(11)) Then Me.iPTaxPercentage = Nothing Else Me.iPTaxPercentage = Row(11)
            If IsDBNull(Row(12)) Then Me.iInventoryTaxPercentage = Nothing Else Me.iInventoryTaxPercentage = Row(12)
            If IsDBNull(Row(13)) Then Me.iRemarks = Nothing Else Me.iRemarks = Row(13)
            If IsDBNull(Row(14)) Then Me.iAlteredByUser = Nothing Else Me.iAlteredByUser = Row(14)
            If IsDBNull(Row(15)) Then Me.iAlteredDate = Nothing Else Me.iAlteredDate = Row(15)
            If IsDBNull(Row(16)) Then Me.iSequence = Nothing Else Me.iSequence = Row(16)
            If IsDBNull(Row(17)) Then Me.iLMUserId = Nothing Else Me.iLMUserId = Row(17)
            If IsDBNull(Row(18)) Then Me.iLMDate = Nothing Else Me.iLMDate = Row(18)
            If Row.Table.Columns.Count > 19 Then
                MyBase.Load(Row, 19)
            End If
            Return True
        End Function
        Protected Friend Overrides Function Load() As Boolean
            'sp_[TableName]Get
            Dim resBool As Boolean = True
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Get '" & Me.iId.ToString("D") & "'")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction

            Dim dt As DataTable = New DataTable
            dt = Utility.GetList(cmd)

            If dt.Rows.Count > 0 Then
                Me.Load(dt.Rows(0))
            Else
                resBool = False
            End If

            Return resBool
        End Function
        Protected Friend Overrides Function Insert() As Boolean
            'sp_[TableName]Insert
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter

            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Insert")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If (Me.iId = Guid.Empty) Then
                        .AddWithValue("@InventoryId", System.DBNull.Value)
                    Else
                        .AddWithValue("@InventoryId", Me.iId)
                    End If
                    If (Me.iStockItemId = Guid.Empty) Then
                        .AddWithValue("@StockItemId", System.DBNull.Value)
                    Else
                        .AddWithValue("@StockItemId", Me.iStockItemId)
                    End If
                    If (Me.iRefType = String.Empty) Then
                        .AddWithValue("@RefType", System.DBNull.Value)
                    Else
                        .AddWithValue("@RefType", Me.iRefType)
                    End If
                    If (Me.iRefId = Guid.Empty) Then
                        .AddWithValue("@RefId", System.DBNull.Value)
                    Else
                        .AddWithValue("@RefId", Me.iRefId)
                    End If
                    .AddWithValue("@PQuantity", Me.iPQuantity)
                    .AddWithValue("@Quantity", Me.iInventoryQuantity)
                    If Me.iCostCalculationMethod = String.Empty Then
                        .AddWithValue("@CostCalculationMethod", System.DBNull.Value)
                    Else
                        .AddWithValue("@CostCalculationMethod", Me.iCostCalculationMethod)
                    End If
                    .AddWithValue("@PCostPrice", Me.iPCostPrice)
                    .AddWithValue("@CostPrice", Me.iInventoryCostPrice)
                    .AddWithValue("@PSellingPrice", Me.iPSellingPrice)
                    .AddWithValue("@SellingPrice", Me.iInventorySellingPrice)
                    .AddWithValue("@PTaxPercentage", Me.iPTaxPercentage)
                    .AddWithValue("@TaxPercentage", Me.iInventoryTaxPercentage)
                    If (Me.iRemarks = String.Empty) Then
                        .AddWithValue("@Remarks", System.DBNull.Value)
                    Else
                        .AddWithValue("@Remarks", Me.iRemarks)
                    End If
                    If (Me.iAlteredByUser = String.Empty) Then
                        .AddWithValue("@AlteredByUser", System.DBNull.Value)
                    Else
                        .AddWithValue("@AlteredByUser", Me.iAlteredByUser)
                    End If
                    If (Me.iAlteredDate = DateTime.MinValue) Then
                        .AddWithValue("@AlteredDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@AlteredDate", Me.iAlteredDate)
                    End If
                    .AddWithValue("@Sequence", Me.iSequence)
                    If (Me.iLMUserId = Guid.Empty) Then
                        .AddWithValue("@LMUserId", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMUserId", Me.iLMUserId)
                    End If
                    If (Me.iLMDate = DateTime.MinValue) Then
                        .AddWithValue("@LMDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMDate", Me.iLMDate)
                    End If
                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw ex
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to insert, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function Update() As Boolean
            'sp_[TableName]Update
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Update")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If (Me.iId = Guid.Empty) Then
                        .AddWithValue("@InventoryId", System.DBNull.Value)
                    Else
                        .AddWithValue("@InventoryId", Me.iId)
                    End If
                    If (Me.iStockItemId = Guid.Empty) Then
                        .AddWithValue("@StockItemId", System.DBNull.Value)
                    Else
                        .AddWithValue("@StockItemId", Me.iStockItemId)
                    End If
                    If (Me.iRefType = String.Empty) Then
                        .AddWithValue("@RefType", System.DBNull.Value)
                    Else
                        .AddWithValue("@RefType", Me.iRefType)
                    End If
                    If (Me.iRefId = Guid.Empty) Then
                        .AddWithValue("@RefId", System.DBNull.Value)
                    Else
                        .AddWithValue("@RefId", Me.iRefId)
                    End If
                    .AddWithValue("@PQuantity", Me.iPQuantity)
                    .AddWithValue("@Quantity", Me.iInventoryQuantity)
                    If Me.iCostCalculationMethod = String.Empty Then
                        .AddWithValue("@CostCalculationMethod", System.DBNull.Value)
                    Else
                        .AddWithValue("@CostCalculationMethod", Me.iCostCalculationMethod)
                    End If
                    .AddWithValue("@PCostPrice", Me.iPCostPrice)
                    .AddWithValue("@CostPrice", Me.iInventoryCostPrice)
                    .AddWithValue("@PSellingPrice", Me.iPSellingPrice)
                    .AddWithValue("@SellingPrice", Me.iInventorySellingPrice)
                    .AddWithValue("@PTaxPercentage", Me.iPTaxPercentage)
                    .AddWithValue("@TaxPercentage", Me.iInventoryTaxPercentage)
                    If (Me.iRemarks = String.Empty) Then
                        .AddWithValue("@Remarks", System.DBNull.Value)
                    Else
                        .AddWithValue("@Remarks", Me.iRemarks)
                    End If
                    If (Me.iAlteredByUser = String.Empty) Then
                        .AddWithValue("@AlteredByUser", System.DBNull.Value)
                    Else
                        .AddWithValue("@AlteredByUser", Me.iAlteredByUser)
                    End If
                    If (Me.iAlteredDate = DateTime.MinValue) Then
                        .AddWithValue("@AlteredDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@AlteredDate", Me.iAlteredDate)
                    End If
                    .AddWithValue("@Sequence", Me.iSequence)
                    If (Me.iLMUserId = Guid.Empty) Then
                        .AddWithValue("@LMUserId", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMUserId", Me.iLMUserId)
                    End If
                    If (Me.iLMDate = DateTime.MinValue) Then
                        .AddWithValue("@LMDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMDate", Me.iLMDate)
                    End If
                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw ex
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to update, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function ValidateOnDelete() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnInsert() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnUpdate() As Boolean
            Return True
        End Function
#End Region

#Region "Overrides"
        Public Overrides Function ToString() As String
            Return Me.iId.ToString
        End Function
        Public Overrides Function ToDetailsListBoxItem() As WindowsExtendedControls.DetailListBoxItem
            Return New WindowsExtendedControls.DetailListBoxItem(Me.ToString)
        End Function
#End Region

    End Class

    Public Class InventoryTypeCollection
        Inherits ObjectModel.Collection(Of InventoryType)

        Public Property Connection As SqlConnection
        Public Property Transaction As SqlTransaction

        Public DataFilters As New DataFilterTypeCollection(Of InventoryTypeFields)("STK_Inventory")
        Public DataSorters As New DataSorterTypeCollection(Of InventoryTypeFields)("STK_Inventory")

        Protected Friend Overloads Function Load() As Boolean
            Return Me.Load("SELECT [STK_Inventory].* " & _
                           "FROM [STK_Inventory] " & _
                           "    INNER JOIN [STK_Stock] ON [STK_Inventory].[StockItemId] = [STK_Stock].[StockItemId] " & _
                           Me.DataFilters.GetSQL & Me.DataSorters.GetSQL)
        End Function

        Protected Friend Overloads Function Load(ByVal CustomFilter As String) As Boolean
            'Get data from DB
            Dim cmd As New SqlCommand
            cmd.Connection = Me.Connection
            If IsNothing(Me.Transaction) = False Then cmd.Transaction = Me.Transaction
            Dim dt As DataTable
            Try
                cmd.CommandText = CustomFilter
                dt = Utility.GetList(cmd)
            Catch ex As Exception
                Throw ex
            End Try
            'Clear exising list
            Dim row As DataRow
            Me.Clear()

            'Build new list
            Dim typeObject As InventoryType

            For Each row In dt.Rows
                typeObject = New InventoryType()
                typeObject.iConnection = Me.Connection
                typeObject.iTransaction = Me.Transaction
                If typeObject.Load(row) = True Then Me.Add(typeObject)
            Next
            Return True
        End Function

    End Class

#Region "Enumerations"       'Enumerations
    Public Enum InventoryTypeFields
        InventoryId
        StockItemId
        RefType
        RefId
        PQuantity
        Quantity
        CostCalculationMethod
        PCostPrice
        CostPrice
        PSellingPrice
        SellingPrice
        PTaxPercentage
        TaxPercentage
        Remarks
        AlteredByUser
        AlteredDate
        Sequence
        LMUserId
        LMDate

    End Enum
#End Region

End Namespace